home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr26 / netprog.zip / NETPROG.TAR / lib.s5 / pty.c < prev    next >
C/C++ Source or Header  |  1989-12-17  |  2KB  |  80 lines

  1. /*
  2.  * Pseudo-terminal routines for Unix System V Release 3.2.
  3.  */
  4.  
  5. #include    <stdio.h>
  6. #include    <fcntl.h>
  7. #include    <stropts.h>
  8.  
  9. #define    PTY_MASTER    "/dev/ptmx"    /* System V Release 3.2 */
  10.  
  11. int            /* returns the file descriptor, or -1 on error */
  12. pty_master()
  13. {
  14.     int    master_fd;
  15.  
  16.     /*
  17.      * Open the master half - "/dev/ptms".  This is a streams clone
  18.      * device, so it'll allocate the first available pty master.
  19.      */
  20.  
  21.     if ( (master_fd = open(PTY_MASTER, O_RDWR)) < 0)
  22.         return(-1);
  23.  
  24.     return(master_fd);
  25. }
  26.  
  27. /*
  28.  * Open the slave half of a pseudo-terminal.
  29.  */
  30.  
  31. int            /* returns the file descriptor, or -1 on error */
  32. pty_slave(master_fd)
  33. int    master_fd;        /* from pty_master() */
  34. {
  35.     int    slave_fd;
  36.     char    *slavename;
  37.     int    grantpt();    /* undocumented function - libpt.a */
  38.     int    unlockpt();    /* undocumented function - libpt.a */
  39.     char    *ptsname();    /* undocumented function - libpt.a */
  40.  
  41.     if (grantpt(master_fd) < 0) {    /* change permissions of slave */
  42.         close(master_fd);
  43.         return(-1);
  44.     }
  45.  
  46.     if (unlockpt(master_fd) < 0) {    /* unlock slave */
  47.         close(master_fd);
  48.         return(-1);
  49.     }
  50.  
  51.     slavename = ptsname(master_fd);    /* determine the slave's name */
  52.     if (slavename == NULL) {
  53.         close(master_fd);
  54.         return(-1);
  55.     }
  56.  
  57.     slave_fd = open(slavename, O_RDWR);    /* open the slave */
  58.     if (slave_fd < 0) {
  59.         close(master_fd);
  60.         return(-1);
  61.     }
  62.  
  63.     /*
  64.      * Now push two modules onto the slave's stream: "ptem" is the
  65.      * pseudo-terminal hardware emulation module, and "ldterm" is
  66.      * the standard terminal line discipline.
  67.      */
  68.  
  69.     if (ioctl(slave_fd, I_PUSH, "ptem") < 0) {
  70.         close(master_fd);
  71.         return(-1);
  72.     }
  73.     if (ioctl(slave_fd, I_PUSH, "ldterm") < 0) {
  74.         close(master_fd);
  75.         return(-1);
  76.     }
  77.  
  78.     return(slave_fd);
  79. }
  80.